var nm;
var un = $.getUrlParam("username");
var id = $.getUrlParam("id");
var pn; //数目
var pr;
var img;
// proNum price imageUrl
$(document).ready(function () {
    // 获取ID
    // var id = $.getUrlParam("id");


    // 加载数据
    get_product(id);
    // 立即购买单击事件
    $("#buy").click(function (e) { 
        add_cart();
    });
    
    $("#mall").click(function () { 
        addhis();
        backtomall();
    });
});

function get_product(id) {
    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/product/detail/" + id,
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message);
                return false;
            }
            set_product(result.data);
        }
    });
}

function set_product(product) {
    if (!product) {
        return false;
    }

    $(".goods-left img").attr("src", product.imageUrl);
    $(".goods-right .goods-title div").text(product.name);
    
    $(".goods-right .seckill-price b").text(product.price);
    $(".goods-right .goods-price del").text(product.price);
    $(".goods-sales span").text(product.proAmount);
    $(".goods-detail div").text(product.introduction);

    nm=product.name;
    pr=product.price;
    img=product.imageUrl;
    console.log(product.introduction);


}

// function create_order(id) {
//     var stock = $("#stock").val();
//     var amount = parseInt($("#amount").val());

//     if (!amount || amount <= 0 || amount > stock) {
//         alertBox("请输入正确的数量！");
//         return false;
//     }

//     $.ajax({
//         type: "POST",
//         url: SERVER_PATH + "/order/create",
//         data: {
//             "itemId": id,
//             "amount": amount,
//         },
//         xhrFields: {withCredentials: true},
//         success: function (result) {
//             if (result.status) {
//                 alertBox(result.data.message, function(){
//                     if (result.data.code == "101") window.location.href = "./login.html";
//                 });
//                 return false;
//             }
//             alertBox("下单成功！", function(){
//                 window.location.href = "./seckill.html"
//             });
//         }
//     });    
// }



function add_cart() {

    pn = parseInt($("#amount").val());


    $.ajax({
        type: "POST",
        url: SERVER_PATH + "/cart/addproduct",
        data: {
            "proId": id,
            "proNum": pn,
            "price":pr,
            "username":un,
            "imageUrl":img,
            "name":nm,
        },
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message, function(){
                    if (result.data.code == "101") window.location.href = "./login.html";
                });
                return false;
            }
            alertBox("加入成功！", function(){
                window.location.href = "seckill.html?username="+un;
            });
        }
    });    
}
function addhis(){
    $.ajax({
        type: "POST",
        url: SERVER_PATH + "/history/addhistory",
        data: {
            "username": un,
            "proId": id,
            "name":nm,
        },
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message, function(){
                    if (result.data.code == "101") window.location.href = "./login.html";
                });
                return false;
            }
           alert(nm);
            
        }
    });    
}


function backtomall(){
    // var un = $.getUrlParam("username");
    // console.log(un);
    //  $(document).find("button").attr("href","seckill.html?username="+gname);
     window.location.href = "seckill.html?username="+un;

}